package com.battery.system.service.battery.impl;

import com.battery.common.core.text.Convert;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.PointsConvertBO;
import com.battery.system.domain.Partner;
import com.battery.system.domain.PointsConvert;
import com.battery.system.domain.VO.PointsConvertVO;
import com.battery.system.mapper.PartnerMapper;
import com.battery.system.mapper.PointsConvertMapper;
import com.battery.system.service.battery.IPointsConvertService;
import com.battery.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 积分兑换订单Service业务层处理
 *
 * @author battery
 * @date 2021-03-24
 */
@Service
public class PointsConvertServiceImpl extends BaseServiceImpl<PointsConvert> implements IPointsConvertService {
    @Autowired
    private PointsConvertMapper pointsConvertMapper;


    @Autowired
    private PartnerMapper partnerMapper;


    /**
     * 查询积分兑换订单
     *
     * @param id 积分兑换订单ID
     * @return 积分兑换订单
     */
    @Override
    public PointsConvert selectPointsConvertById(String id) {
        return pointsConvertMapper.selectPointsConvertById(id);
    }

    /**
     * 查询积分兑换订单列表
     *
     * @param pointsConvert 积分兑换订单
     * @return 积分兑换订单
     */
    @Override
    public List<PointsConvert> selectPointsConvertList(PointsConvert pointsConvert) {
        return pointsConvertMapper.selectPointsConvertList(pointsConvert);
    }

    /**
     * 新增积分兑换订单
     *
     * @param pointsConvert 积分兑换订单
     * @return 结果
     */
    @Override
    public int insertPointsConvert(PointsConvert pointsConvert) {
        pointsConvert.setCreateTime(DateUtils.getNowDate());
        return pointsConvertMapper.insertPointsConvert(pointsConvert);
    }

    /**
     * 修改积分兑换订单
     *
     * @param pointsConvert 积分兑换订单
     * @return 结果
     */
    @Override
    public int updatePointsConvert(PointsConvert pointsConvert) {
        pointsConvert.setUpdateTime(DateUtils.getNowDate());
        return pointsConvertMapper.updatePointsConvert(pointsConvert);
    }

    /**
     * 删除积分兑换订单对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePointsConvertByIds(String ids) {
        return pointsConvertMapper.deletePointsConvertByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除积分兑换订单信息
     *
     * @param id 积分兑换订单ID
     * @return 结果
     */
    @Override
    public int deletePointsConvertById(String id) {
        return pointsConvertMapper.deletePointsConvertById(id);
    }

    /**
     * 展示积分兑换列表数据
     *
     * @param pointsConvertBO
     * @return
     */
    @Override
    public List<PointsConvertVO> selectPointsConvertVOList(PointsConvertBO pointsConvertBO) {
        return pointsConvertMapper.selectPointsConvertVOList(pointsConvertBO);
    }

    /**
     * 展示积分兑换对应的商品列表
     *
     * @param pointsConvertBO
     * @return
     */
    @Override
    public List<PointsConvertVO> selectPointsConvertItemsVOList(PointsConvertBO pointsConvertBO) {
        return pointsConvertMapper.selectPointsConvertItemsVOList(pointsConvertBO);
    }


 	@Override
    public void updatePointsByOutOrder(PointsConvert pointsConvert) {
        pointsConvertMapper.updatePointsByOutOrder(pointsConvert);
    }

    @Override
    public PointsConvert selectPointsConvertByTrandeNo(String tradeNo) {
        return pointsConvertMapper.selectPointsConvertByTrandeNo(tradeNo);
    }

    /**
     * 拒绝审核  - 积分兑换
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public int nopass(String id) {
        // 拒绝之后 需要将积分加到用户上，TODO TODO 暂时不加入积分流水表 同时生成一笔押金日志记录
        PointsConvert convert = pointsConvertMapper.selectByPrimaryKey(id);

        Date date = DateUtils.getNowDate();
        Partner partner = partnerMapper.selectByPrimaryKey(convert.getPartnerId());
        Partner partnerUpdate = new Partner();
        partnerUpdate.setId(partner.getId());
        partnerUpdate.setPoint(partner.getPoint() + convert.getRealPayAmount());
        partnerUpdate.setUpdateTime(date);
        partnerMapper.updateByPrimaryKeySelective(partnerUpdate);

        PointsConvert convertUpdate = new PointsConvert();
        convertUpdate.setId(convert.getId());
        convertUpdate.setApprovalTime(date);
        convertUpdate.setStatus(3L); // 拒绝审核

        return pointsConvertMapper.updateByPrimaryKeySelective(convertUpdate);
    }}
